import requests

server= 'http://127.0.0.1:8888'

import  gol
def http_get(path, data, addToken = True):
    try:
        if addToken == True:
            data['temp'] = gol.get_value('temp')
            data['temp_user'] = gol.get_value('username')
# 定义请求头
        headers = {        
        'Accept': 'application/json',  # 指定接受的数据格式为JSON
        'temp': 'application/json'  # 指定接受的数据格式为JSON
        # 其他请求头...
        }
        headers["temp"] = gol.get_value_str('temp')
        headers["temp_user"] = gol.get_value_str('username')
        headers["Authorization"] = "Bearer "+gol.get_value_str('temp')
        print(headers)
        response = requests.get('%s/%s' % (server,path),  headers=headers,params=data,verify=False)
        response.raise_for_status()  # 检查HTTP状态码是否为200
        return response.text
    except requests.exceptions.Timeout:
        print("请求超时")
        return '0'
    except requests.exceptions.ConnectionError:
        print("网络连接错误")
        return '0'
    except requests.exceptions.HTTPError as http_error:
        print(f"HTTP错误: {http_error}")
        return '0'
    except requests.exceptions.RequestException as e:
        print(f"其他异常: {e}")
        return '0'

def http_post(path, data, addToken = True):
    try:
        if addToken == True:
            data['temp'] = gol.get_value('temp')
            data['temp_user'] = gol.get_value('username')
            print(data)
            
        headers = {        
        'Accept': 'application/json',  # 指定接受的数据格式为JSON
        'temp': 'application/json'  # 指定接受的数据格式为JSON
        # 其他请求头...
        }
        headers["temp"] = gol.get_value_str('temp')
        headers["temp_user"] = gol.get_value_str('username')
        headers["Authorization"] = "Bearer "+gol.get_value_str('temp')
        print(headers)
        response = requests.post('%s/%s' % (server,path), headers=headers, data=data,verify=False)
        response.raise_for_status()  # 检查HTTP状态码是否为200
        return response.text
    except requests.exceptions.Timeout:
        print("请求超时")
        return '0'
    except requests.exceptions.ConnectionError:
        print("网络连接错误")
        return '0'
    except requests.exceptions.HTTPError as http_error:
        print(f"HTTP错误: {http_error}")
        return '0'
    except requests.exceptions.RequestException as e:
        print(f"其他异常: {e}")
        return '0'
